package com.sayimo.maker.mall;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.api.AlipayConstants;
import com.alipay.api.internal.util.AlipaySignature;
import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.commons.ali.pay.ALiTransfer;
import com.sayimo.maker.commons.ali.pay.AlipayUtil;
import com.sayimo.maker.mall.orders.dao.OrdersListInterfaceDao;
import com.sayimo.maker.mall.orders.service.OrdersListInterfaceService;

@Controller
@RequestMapping("/orders")
public class OrdersController {

	private static Log logger = LogFactory.getLog(OrdersController.class);
	@Resource(name = "ordersListInterfaceService")
	private OrdersListInterfaceService ordersListInterfaceService;

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:insertPreOrders
	 * @Description:从物品下单
	 * @param request
	 * @return
	 * @Date 2017年1月9日,下午5:23:30
	 */
	@RequestMapping(value = "/insertnormalorders", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertPreOrders(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.insertOrdersByGoods(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:updateQueryOrdersPayDetails
	 * @Description:订单支付详情
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 * @Date 2017年1月9日,下午5:44:39
	 */
	@RequestMapping(value = "/orderpaydetails/{orderIds}/{customerId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> updateQueryOrdersPayDetails(@PathVariable("orderIds") String orderIds,
			@PathVariable("customerId") Integer customerId) throws UnsupportedEncodingException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			orderIds = URLDecoder.decode(orderIds, "UTF-8");
			resultMap = ordersListInterfaceService.updateQueryOrdersPayDetails(orderIds, customerId);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:queryOrdersList
	 * @Description:
	 * @param customerId
	 * @param status
	 * @param pageSize
	 * @param pageNow
	 * @return
	 * @Date 2017年1月9日,下午7:05:40
	 */
	@RequestMapping(value = "/getorderslist/{pageSize}/{pageNow}/{mallType}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryOrdersList(HttpServletRequest request,
			@RequestParam(value = "status", required = false) Integer status,
			@RequestParam(value = "orderType", required = false) Integer orderType,
			@PathVariable("pageSize") Integer pageSize, @PathVariable("pageNow") Integer pageNow,
			@PathVariable("mallType") Integer mallType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Integer customerId = (Integer) request.getAttribute("customerId");
			resultMap = ordersListInterfaceService.editMemberCustomerOrderList(customerId, status, pageSize, pageNow,
					mallType, orderType);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:insertOrdersByShoopingCart
	 * @Description:购物车下单
	 * @param request
	 * @return
	 * @Date 2017年1月9日,下午5:26:52
	 */
	@RequestMapping(value = "/insertordersbyshoopingcart", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertOrdersByShoopingCart(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.insertOrdersByShoopingCart(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:queryOrdersDetails
	 * @Description:订单详情
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 * @Date 2017年1月9日,下午5:28:43
	 */
	@RequestMapping(value = "/orderdetails/{orderIds}/{customerId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> queryOrdersDetails(@PathVariable("orderIds") String orderIds,
			@PathVariable("customerId") Integer customerId) throws UnsupportedEncodingException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			orderIds = URLDecoder.decode(orderIds, "UTF-8");
			resultMap = ordersListInterfaceService.queryOrdersDetails(orderIds, customerId);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:updateConfirmReceive
	 * @Description:确认收货
	 * @param customerId
	 * @param orderId
	 * @return
	 * @Date 2017年1月9日,下午5:35:30
	 */
	@RequestMapping(value = "/confirmreceive", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateConfirmReceive(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMaps = new HashMap<String, Object>();
		try {
			Integer customerId = (Integer) request.getAttribute("customerId");
			Integer orderId = Integer.valueOf(request.getParameter("orderId"));
			paramMaps.put("customerId", customerId);
			paramMaps.put("orderId", orderId);
			resultMap = ordersListInterfaceService.updateConfirmReceive(paramMaps);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:cancelOrder
	 * @Description:取消订单
	 * @param request
	 * @return
	 * @Date 2017年1月9日,下午5:48:41
	 */
	@RequestMapping(value = "/cancelorder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> cancelOrder(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.deleteOrder(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:updatePayOrder
	 * @Description:支付
	 * @param request
	 * @return
	 * @throws Exception
	 * @Date 2017年1月11日,上午11:15:39
	 */
	@RequestMapping(value = "/payorder", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updatePayOrder(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.updatePayOrder(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:getLogisticInfo
	 * @Description:订单物流信息
	 * @param orderId
	 * @param customerId
	 * @return
	 * @throws DaoException
	 * @Date 2017年1月16日,下午2:58:42
	 */
	@RequestMapping(value = "/getlogisticinfo/{orderId}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getLogisticInfo(@PathVariable("orderId") Integer orderId,
			HttpServletRequest request) throws DaoException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Integer customerId = (Integer) request.getAttribute("customerId");
			resultMap = ordersListInterfaceService.getLogisticInfo(orderId, customerId);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Title: updatePaySuccess
	 * @Author: wangyang
	 * @Date:日期：2017年2月9日 时间：下午16:05
	 * @Description: 支付成功回调
	 * @return String
	 * @Version 1.0.0
	 */
	@RequestMapping(value = "/updatepaysuccess", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updatePaySuccess() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.updatePaySuccess();
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.rainbow.web.orders
	 * @Author 龚益君
	 * @MethodName:addOrderReceiveAddress
	 * @Description:新增订单收货地址
	 * @param request
	 * @return
	 * @Date 2017年1月9日,下午5:47:29
	 */
	@RequestMapping(value = "/addorderreceiveaddress", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addOrderReceiveAddress(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = ordersListInterfaceService.addOrderReceiveAddress(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * 
	 * @MethodName callbackALiPay
	 * @Description 支付宝异步回调
	 * @author wangyang
	 * @date 2017年3月1日 下午15:20:22
	 * @param
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/callbackalipay")
	@ResponseBody
	public String callbackALiPay() throws Exception {
		String callbackStatus = "error";
		try {
			callbackStatus = ordersListInterfaceService.callbackALiPay();
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
		return callbackStatus;

	}

	/**
	 * 
	 * @MethodName aliTransfer
	 * @Description 支付宝转账
	 * @author wangyang
	 * @date 2017年3月2日 下午17:06:05
	 * @param
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/alitransfer", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> aliTransfer(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("payee_account", request.getParameter("payee_account")); // 收款方账户
			paramMap.put("payPrice", request.getParameter("payPrice")); // 转账金额
			paramMap.put("payee_real_name", request.getParameter("payee_real_name")); // 收款方真实姓名
			paramMap.put("remark", request.getParameter("remark")); // 转账备注

			resultMap = ALiTransfer.aliTransfer(paramMap);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;
	}

	/**
	 * 
	 * @MethodName validAliTransfer
	 * @Description 查询转账是否支付成功
	 * @author wangyang
	 * @date 2017年3月2日 下午17:41:05
	 * @param
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/validalitransfer/{out_biz_no}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> validAliTransfer(@PathVariable("out_biz_no") String out_biz_no) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {

			resultMap = ALiTransfer.validAliTransfer(out_biz_no);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			resultMap.put(Constant.STATUS, Constant.STATUS_ERROR);
			resultMap.put(Constant.ERROR_CODE, Constant.SYSTEM_ERROR);
		}
		return resultMap;

	}

	/**
	 * @Package com.sayimo.school.mall.module.provider.controller
	 * @Author 龚益君
	 * @MethodName:providerconfirmsendgoods
	 * @Description:商家发货
	 * @param request
	 * @return
	 * @Date 2016年11月9日,下午1:53:29
	 */
	@RequestMapping(value = "/providerconfirmsendgoods", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> providerconfirmsendgoods(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap = ordersListInterfaceService.providerConfirmSendGoods(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * @Package com.sayimo.maker.mall
	 * @Author 龚益君
	 * @MethodName:providerReimburse
	 * @Description:商家同意退款
	 * @param request
	 * @return
	 * @Date 2017年3月6日,上午9:52:45
	 */
	@RequestMapping(value = "/providerreimburse", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> providerReimburse(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap = ordersListInterfaceService.providerReimburse(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
		}
		return resultMap;
	}
	 /**
	 * @Package com.sayimo.maker.mall
	 * @Author 龚益君
	 * @MethodName:sayimoReimburse
	 * @Description:平台同意退款
	 * @param request
	 * @return
	 * @Date 2017年3月7日,下午3:45:13
	 */
	@RequestMapping(value = "/sayimoreimburse", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> sayimoReimburse(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<>();
		try {
			resultMap = ordersListInterfaceService.sayimoReimburse(request);
		} catch (ServiceException e) {
			logger.error(e);
			e.printStackTrace();
		}
		return resultMap;
	}
}
